(function(){"use strict";BX.namespace("BX.Landing.UI.Field");var i=BX.Landing.Utils.isPlainObject;var t=BX.Landing.Utils.isNumber;var e=BX.Landing.Utils.isEmpty;var n=BX.Landing.Utils.isString;var s=BX.Landing.Utils.decodeDataValue;var a=BX.Landing.Utils.clone;var o=BX.Landing.Utils.create;var d=BX.Landing.Utils.fireCustomEvent;BX.Landing.UI.Field.Image=function(t){BX.Landing.UI.Field.Text.apply(this,arguments);this.dimensions=typeof t.dimensions==="object"?t.dimensions:null;this.create2xByDefault=t.create2xByDefault!==false;this.uploadParams=typeof t.uploadParams==="object"?t.uploadParams:{};this.onValueChangeHandler=t.onValueChange?t.onValueChange:function(){};this.type=this.content.type||"image";this.allowClear=t.allowClear;this.input.innerText=this.content.src;this.input.hidden=true;this.input2x=this.createInput();this.input2x.innerText=this.content.src2x;this.input2x.hidden=true;this.layout.classList.add("landing-ui-field-image");if(t.compactMode===true){this.layout.classList.add("landing-ui-field-image--compact")}this.disableAltField=typeof t.disableAltField==="boolean"?t.disableAltField:false;this.fileInput=r(this.selector);this.fileInput.addEventListener("change",this.onFileInputChange.bind(this));this.linkInput=l();this.linkInput.onInputHandler=this.onLinkInput.bind(this);this.dropzone=h(this.selector);this.dropzone.hidden=true;this.dropzone.insertBefore(this.fileInput,this.dropzone.firstElementChild);this.onDragOver=this.onDragOver.bind(this);this.onDragLeave=this.onDragLeave.bind(this);this.onDrop=this.onDrop.bind(this);this.dropzone.addEventListener("dragover",this.onDragOver);this.dropzone.addEventListener("dragleave",this.onDragLeave);this.dropzone.addEventListener("drop",this.onDrop);this.clearButton=u();this.clearButton.on("click",this.onClearClick.bind(this));this.preview=c();this.preview.appendChild(this.clearButton.layout);this.preview.style.backgroundImage="url("+this.input.innerText.trim()+")";this.onImageDragEnter=this.onImageDragEnter.bind(this);this.preview.addEventListener("dragenter",this.onImageDragEnter);this.loader=new BX.Loader({target:this.preview});this.icon=p();this.image=g();this.image.appendChild(this.preview);this.image.appendChild(this.icon);this.image.dataset.fileid=this.content.id;this.image.dataset.fileid2x=this.content.id2x;this.hiddenImage=o("img",{props:{className:"landing-ui-field-image-hidden"}});if(i(this.content)&&"src"in this.content){this.hiddenImage.src=this.content.src}this.altField=f();this.altField.setValue(this.content.alt);this.left=m();this.left.appendChild(this.dropzone);this.left.appendChild(this.image);this.left.appendChild(this.hiddenImage);if(this.description){this.left.appendChild(this.description)}this.left.appendChild(this.altField.layout);this.left.appendChild(this.linkInput.layout);this.uploadButton=L();this.uploadButton.on("click",this.onUploadClick.bind(this));this.editButton=v();this.editButton.on("click",this.onEditClick.bind(this));this.right=B();this.right.appendChild(this.uploadButton.layout);this.right.appendChild(this.editButton.layout);this.form=I();this.form.appendChild(this.left);this.form.appendChild(this.right);this.layout.appendChild(this.form);this.enableTextOnly();if(!this.input.innerText.trim()||this.input.innerText.trim()===window.location.toString()){this.showDropzone()}if(this.disableAltField){this.altField.layout.hidden=true;this.altField.layout.style.display="none";this.altField.layout.classList.add("landing-ui-hide")}if(this.content.type==="icon"){this.type="icon";this.classList=this.content.classList;var e=this.content.classList;var n=[];BX.Landing.UI.Panel.IconPanel.getLibraries().then(function(i){i.forEach(function(i){i.categories.forEach(function(i){i.items.forEach(function(i){var t=i.split(" ");t.forEach(function(i){if(e.indexOf(i)!==-1&&n.indexOf(i)===-1){n.push(i)}})})})});this.icon.innerHTML='<span class="'+n.join(" ")+'"></span>'}.bind(this));this.showPreview();this.altField.layout.hidden=true}this.makeAsLinkWrapper=o("div",{props:{className:"landing-ui-field-image-make-as-link-wrapper"},children:[o("div",{props:{className:"landing-ui-field-image-make-as-link-button"},children:[]})]});this.url=new BX.Landing.UI.Field.Link({content:this.content.url||{text:"",href:""},options:{siteId:BX.Landing.Main.getInstance().options.site_id,landingId:BX.Landing.Main.getInstance().id},contentRoot:this.contentRoot});this.urlCheckbox=o("input",{props:{type:"checkbox"},attrs:{style:"margin-left: 4px;"}});function s(i,t){if(i.checked){t.querySelector(".landing-ui-field-link-right").classList.remove("landing-ui-disabled");t.querySelector(".landing-ui-field-link-url-grid").classList.remove("landing-ui-disabled")}else{t.querySelector(".landing-ui-field-link-right").classList.add("landing-ui-disabled");t.querySelector(".landing-ui-field-link-url-grid").classList.add("landing-ui-disabled")}}this.urlCheckbox.addEventListener("change",function(){s(this.urlCheckbox,this.url.layout)}.bind(this));this.urlCheckbox.checked=this.content.url&&this.content.url.enabled;s(this.urlCheckbox,this.url.layout);this.url.hrefInput.header.appendChild(this.urlCheckbox);this.url.left.hidden=true;this.makeAsLinkWrapper.appendChild(this.url.layout);if(!t.disableLink){this.layout.appendChild(this.makeAsLinkWrapper)}this.content=this.getValue();BX.DOM.write(function(){this.adjustPreviewBackgroundSize()}.bind(this));if(this.getValue().type==="background"||this.allowClear){this.clearButton.layout.classList.add("landing-ui-show")}this.uploader=new BX.Landing.ImageUploader({uploadParams:this.uploadParams,additionalParams:{context:"imageeditor"},dimensions:this.dimensions,sizes:["1x","2x"]});this.adjustEditButtonState()};function r(i){return BX.create("input",{props:{className:"landing-ui-field-image-dropzone-input"},attrs:{accept:"image/*",type:"file",id:"file_"+i,name:"picture"}})}function l(){var i=new BX.Landing.UI.Field.Text({id:"path_to_image",placeholder:BX.Landing.Loc.getMessage("LANDING_IMAGE_UPLOAD_MENU_LINK_LABEL")});i.enableTextOnly();i.layout.hidden=true;return i}function h(i){return BX.create("label",{props:{className:"landing-ui-field-image-dropzone"},children:[BX.create("div",{props:{className:"landing-ui-field-image-dropzone-text"},html:'<div class="landing-ui-field-image-dropzone-title">'+BX.Landing.Loc.getMessage("LANDING_IMAGE_DROPZONE_TITLE")+"</div>"+'<div class="landing-ui-field-image-dropzone-subtitle">'+BX.Landing.Loc.getMessage("LANDING_IMAGE_DROPZONE_SUBTITLE")+"</div>"})],attrs:{for:"file_"+i}})}function u(){return new BX.Landing.UI.Button.BaseButton("clear",{className:"landing-ui-field-image-action-button-clear"})}function c(){return BX.create("div",{props:{className:"landing-ui-field-image-preview-inner"}})}function p(){return BX.create("span",{props:{className:"landing-ui-field-image-preview-icon"}})}function g(){return BX.create("div",{props:{className:"landing-ui-field-image-preview"}})}function f(){var i=new BX.Landing.UI.Field.Text({placeholder:BX.Landing.Loc.getMessage("LANDING_FIELD_IMAGE_ALT_PLACEHOLDER"),className:"landing-ui-field-image-alt",textOnly:true});return i}function m(){return BX.create("div",{props:{className:"landing-ui-field-image-left"}})}function L(){return new BX.Landing.UI.Button.BaseButton("upload",{text:BX.Landing.Loc.getMessage("LANDING_FIELD_IMAGE_UPLOAD_BUTTON"),className:"landing-ui-field-image-action-button"})}function v(){var i=new BX.Landing.UI.Button.BaseButton("edit",{text:BX.Landing.Loc.getMessage("LANDING_FIELD_IMAGE_EDIT_BUTTON"),className:"landing-ui-field-image-action-button"});return i}function B(){return BX.create("div",{props:{className:"landing-ui-field-image-right"}})}function I(){return BX.create("form",{props:{className:"landing-ui-field-image-container"},attrs:{method:"post",enctype:"multipart/form-data"},events:{submit:function(i){i.preventDefault()}}})}BX.Landing.UI.Field.Image.prototype={constructor:BX.Landing.UI.Field.Image,__proto__:BX.Landing.UI.Field.Text.prototype,superClass:BX.Landing.UI.Field.Text,onInputInput:function(){this.preview.src=this.input.innerText.trim()},onImageDragEnter:function(i){i.preventDefault();i.stopPropagation();if(!this.imageHidden){this.showDropzone();this.imageHidden=true}},onDragOver:function(i){i.preventDefault();i.stopPropagation();this.dropzone.classList.add("landing-ui-active")},onDragLeave:function(i){i.preventDefault();i.stopPropagation();this.dropzone.classList.remove("landing-ui-active");if(this.imageHidden){this.imageHidden=false;this.showPreview()}},onDrop:function(i){i.preventDefault();i.stopPropagation();this.dropzone.classList.remove("landing-ui-active");this.onFileChange(i.dataTransfer.files[0]);this.imageHidden=false},onFileChange:function(i){this.showLoader();this.upload(i).then(this.setValue.bind(this)).then(this.hideLoader.bind(this)).catch(function(i){console.error(i);this.hideLoader()}.bind(this))},onFileInputChange:function(i){this.onFileChange(i.currentTarget.files[0])},onUploadClick:function(i){this.bindElement=i.currentTarget;i.preventDefault();if(!this.uploadMenu){this.uploadMenu=BX.Main.MenuManager.create({id:"upload_"+this.selector+ +new Date,bindElement:this.bindElement,bindOptions:{forceBindPosition:true},items:[{text:BX.Landing.Loc.getMessage("LANDING_IMAGE_UPLOAD_MENU_UNSPLASH"),onclick:this.onUnsplashShow.bind(this)},{text:BX.Landing.Loc.getMessage("LANDING_IMAGE_UPLOAD_MENU_GOOGLE"),onclick:this.onGoogleShow.bind(this)},{text:BX.Landing.Loc.getMessage("LANDING_IMAGE_UPLOAD_MENU_UPLOAD"),onclick:this.onUploadShow.bind(this)},{text:BX.Landing.Loc.getMessage("LANDING_IMAGE_UPLOAD_MENU_LINK"),onclick:this.onLinkShow.bind(this)}],events:{onPopupClose:function(){this.bindElement.classList.remove("landing-ui-active");if(this.uploadMenu){this.uploadMenu.destroy();this.uploadMenu=null}}.bind(this)},targetContainer:this.contentRoot});if(!this.contentRoot){this.bindElement.parentNode.appendChild(this.uploadMenu.popupWindow.popupContainer)}}this.bindElement.classList.add("landing-ui-active");this.uploadMenu.toggle();if(!this.contentRoot){var t=BX.pos(this.bindElement,this.bindElement.parentNode);this.uploadMenu.popupWindow.popupContainer.style.top=t.bottom+"px";this.uploadMenu.popupWindow.popupContainer.style.left="auto";this.uploadMenu.popupWindow.popupContainer.style.right="5px"}},onUnsplashShow:function(){this.uploadMenu.close();BX.Landing.UI.Panel.Image.getInstance().show("unsplash",this.dimensions,this.loader,this.uploadParams).then(this.upload.bind(this)).then(this.setValue.bind(this)).then(this.hideLoader.bind(this)).catch(function(i){console.error(i);this.hideLoader()}.bind(this))},onGoogleShow:function(){this.uploadMenu.close();BX.Landing.UI.Panel.Image.getInstance().show("google",this.dimensions,this.loader,this.uploadParams).then(this.upload.bind(this)).then(this.setValue.bind(this)).then(this.hideLoader.bind(this)).catch(function(i){BX.Landing.ErrorManager.getInstance().add({type:"error",action:"BAD_IMAGE",hideSupportLink:true});console.error(i);this.hideLoader()}.bind(this))},onUploadShow:function(){this.uploadMenu.close();this.fileInput.click()},onLinkShow:function(){this.uploadMenu.close();this.showLinkField();this.linkInput.setValue("")},onEditClick:function(i){i.preventDefault();this.edit({src:this.hiddenImage.src})},onClearClick:function(i){i.preventDefault();this.setValue({src:""});this.fileInput.value="";this.showDropzone()},showDropzone:function(){this.dropzone.hidden=false;this.image.hidden=true;this.altField.layout.hidden=true;this.linkInput.layout.hidden=true},showPreview:function(){this.dropzone.hidden=true;this.image.hidden=false;this.altField.layout.hidden=false;this.linkInput.layout.hidden=true},showLinkField:function(){this.dropzone.hidden=true;this.image.hidden=true;this.altField.layout.hidden=true;this.linkInput.layout.hidden=false},onLinkInput:function(i){var t=BX.create("img");t.src=i;t.onload=function(){this.showPreview();this.setValue({src:i,src2x:i})}.bind(this)},showLoader:function(){if(this.dropzone&&!this.dropzone.hidden){this.loader.show(this.dropzone);return}this.loader.show(this.preview)},hideLoader:function(){this.loader.hide()},onInputClick:function(i){i.preventDefault()},isChanged:function(){var i=a(this.content);var t=a(this.getValue());if(i.url&&n(i.url)){i.url=s(i.url)}if(t.url&&n(t.url)){t.url=s(t.url)}return JSON.stringify(i)!==JSON.stringify(t)},adjustPreviewBackgroundSize:function(){var i=BX.create("img",{attrs:{src:this.getValue().src}});i.onload=function(){var t=this.preview.getBoundingClientRect();var e="cover";if(i.width>t.width||i.height>t.height){e="contain"}if(i.width<t.width&&i.height<t.height){e="auto"}BX.DOM.write(function(){this.preview.style.backgroundSize=e}.bind(this))}.bind(this)},setValue:function(i,t){if(i.type!=="icon"){if(!i||!i.src){this.input.innerText="";this.input2x.innerText="";this.preview.removeAttribute("style");this.input.dataset.ext="";this.showDropzone()}else{this.input.innerText=i.src;this.input2x.innerText=i.src2x||"";this.preview.style.backgroundImage='url("'+(i.src2x||i.src)+'")';this.preview.id=BX.util.getRandomString();this.hiddenImage.src=i.src2x||i.src;this.showPreview()}this.image.dataset.fileid=i&&i.id?i.id:-1;this.image.dataset.fileid2x=i&&i.id2x?i.id2x:-1;this.classList=[]}else{this.preview.style.backgroundImage=null;this.classList=i.classList;this.icon.innerHTML='<span class="'+i.classList.join(" ")+'"></span>';this.showPreview();this.type="icon";this.altField.layout.hidden=true;this.altField.setValue("");this.input.innerText=""}if(i.url){this.url.setValue(i.url)}this.adjustPreviewBackgroundSize();this.adjustEditButtonState();this.hideLoader();this.onValueChangeHandler(this);BX.fireEvent(this.layout,"input");var e=new BX.Event.BaseEvent({data:{value:this.getValue()},compatData:[this.getValue()]});if(!t){this.emit("change",e)}},adjustEditButtonState:function(){var i=this.getValue();if(BX.Type.isStringFilled(i.src)){this.editButton.enable()}else{this.editButton.disable()}},reset:function(){this.setValue({type:this.getValue().type,id:-1,src:"",alt:""})},getValue:function(){var i=parseInt(this.image.dataset.fileid);var t=parseInt(this.image.dataset.fileid2x);i=i===i?i:-1;t=t===t?t:-1;var e={type:"",src:"",id:i,id2x:t,src2x:"",alt:"",url:""};if(this.type==="background"){e.type="background";e.src=this.input.innerText.trim();e.src2x=this.input2x.innerText.trim();e.id=i;e.id2x=t}if(this.type==="image"){e.type="image";e.src=this.input.innerText.trim();e.src2x=this.input2x.innerText.trim();e.id=i;e.id2x=t;e.alt=this.altField.getValue()}if(this.type==="icon"){e.type="icon";e.classList=this.classList}e.url=Object.assign({},this.url.getValue(),{enabled:this.urlCheckbox.checked});return e},edit:function(i){BX.Landing.ImageEditor.edit({image:i.src,dimensions:this.dimensions}).then(function(i){return this.upload(i,{context:"imageEditor"})}.bind(this)).then(function(i){this.setValue(i)}.bind(this));var t=new Image;var e="/bitrix/images/landing/close.svg";e=BX.util.add_url_param(e,{action:"openImageEditor"});t.src=e+"?"+ +new Date},upload:function(i,t){if(i.type&&(i.type.includes("text")||i.type.includes("html"))){BX.Landing.ErrorManager.getInstance().add({type:"error",action:"BAD_IMAGE"});return Promise.reject({type:"error",action:"BAD_IMAGE"})}this.showLoader();var e=new Promise(function(t){var e=["1x","2x"];if(this.create2xByDefault===false){var n=new Image;var s=URL.createObjectURL(i);var a=this.dimensions;n.onload=function(){URL.revokeObjectURL(s);if((this.width>=a.width||this.height>=a.height||this.width>=a.maxWidth||this.height>=a.maxHeight)===false){e=["1x"]}t(e)};n.src=s}else{t(e)}}.bind(this));return e.then(function(e){var n=function(){if(this.create2xByDefault===false&&BX.Type.isArrayFilled(e)){return e}return["1x","2x"]}.bind(this)();return this.uploader.setSizes(n).upload(i,t).then(function(i){this.hideLoader();if(n.length===1){return i[0]}return Object.assign({},i[0],{src2x:i[1].src,id2x:i[1].id})}.bind(this))}.bind(this))}}})();
//# sourceMappingURL=image_field.map.js